package com.gs.SynPatrolRep.util;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.widget.Toast;

import androidx.core.content.FileProvider;

import com.gs.SynPatrolRep.R;
import com.gs.SynPatrolRep.constant.AllConstants;

import java.io.File;

public class PhotoUtil {
    private static volatile PhotoUtil instance;

    public PhotoUtil() {
    }

    public static PhotoUtil getInstance() {
        if (instance == null) {
            synchronized (PhotoUtil.class) {
                if (instance == null) {
                    instance = new PhotoUtil();
                }
            }
        }
        return instance;
    }

    public String imgUrl;

    /**
     * 跳转至相机
     *
     * @param activity
     */
    public void toCamera(final Activity activity) {
        if (PermissionUtil.addPermission(activity, new String[]{Manifest.permission.CAMERA},
                AllConstants.PIC_CAMERA)) {
            if (FileUtils.getInstance().hasSdCard()) {
                PackageManager pm = activity.getPackageManager();
                if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) || pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {
                    try {
                        File photoFile = new File(activity.getExternalCacheDir(), System.currentTimeMillis() + ".jpeg");
                        if (photoFile.exists()) {
                            photoFile.delete();
                        }
                        photoFile.createNewFile();
                        imgUrl = photoFile.getAbsolutePath();
                        Uri imageUri;
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            imageUri = FileProvider.getUriForFile(activity, activity.getPackageName() + ".provider", photoFile);
                        } else {
                            imageUri = Uri.fromFile(photoFile);
                        }
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                        activity.startActivityForResult(intent, AllConstants.PIC_CAMERA);
                    } catch (Exception e) {
                        Toast.makeText(activity, R.string.no_camera, Toast.LENGTH_LONG).show();
                    }
                }
            }
        }
    }

    public Bitmap rotatingPic(int angle, Bitmap bitmap) {
        Bitmap returnBmp = null;
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        try {
            returnBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        } catch (Exception e) {
        }
        if (returnBmp == null) {
            returnBmp = bitmap;
        }

        if (bitmap != null) {
            bitmap.recycle();
        }

        return returnBmp;
    }

    private Camera.CameraInfo info;
    private int angle;

    public int getAngle() {
        if (info == null) {
            info = new Camera.CameraInfo();
            android.hardware.Camera.getCameraInfo(0, info);
            angle = info.orientation;
        }
        return angle;
    }

    /**
     * 跳转至图库
     *
     * @param activity
     */
    public void toGallery(final Activity activity) {
        if (PermissionUtil.addPermission(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE}, AllConstants.PIC_GALLERY)) {
            try {
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                activity.startActivityForResult(intent, AllConstants.PIC_GALLERY);
            } catch (Exception e) {
                Toast.makeText(activity, R.string.no_gallery, Toast.LENGTH_LONG).show();
            }
        }
    }
}
